#include "Client.h"

bool ClientPtr::NewClient(int ifd)
{
    pClnt = new CClient(ifd);

    if (nullptr != pClnt)
    {
        pClnt->Init();
        return true;
    }

    return false;
}

void ClientPtr::DestroyClient()
{
    if (nullptr != pClnt)
    {
        delete pClnt;
        pClnt = nullptr;
    }
}

CClient::CClient(int ifd) : m_ifd(ifd)
{
    m_bFirst = true;
    m_bSsl = false;
    m_eProt = ProtType::Invalide;
    m_nMod = 0;
    m_tLast = 0;
    m_pClntNode = nullptr;
}

CClient::~CClient()
{
    Destroy();
}

void *CClient::operator new(size_t sz)
{
    CMemPool *mp = CMemPool::GetInstance();

    if (nullptr == mp)
        return NULL;

    return mp->Alloc(sz);
}

void CClient::operator delete(void *ptr)
{
    CMemPool *mp = CMemPool::GetInstance();

    if (nullptr != mp)
        mp->Free(ptr);
}

bool CClient::Init()
{
    m_bufRecv.SetSocket(m_ifd);
    m_bufSend.SetSocket(m_ifd);

    return true;
}

void CClient::Destroy()
{
    if (-1 != m_ifd)
        close(m_ifd);

    m_bufRecv.Destroy();
    m_bufSend.Destroy();
}